#include "paramsetui.h"
#include "ui_paramsetui.h"
#include "constants.h"
#include "paramui.h"
#include "sqlite3.h"
#include <QTimer>
#include <QDateTime>

ParamSetUI::ParamSetUI(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ParamSetUI)
{
    ui->setupUi(this);
    setWindowFlags(this->windowFlags() | Qt::FramelessWindowHint );
    //load logo and background
    QPixmap lg(logoStr);
    ui->logo->setPixmap(lg);
    QPixmap bg(backgroundStr);
    QPalette background;
    background.setBrush(QPalette::Background, QBrush(bg));
    setPalette(background);
    QPalette palette = ui->data->palette();
    palette.setColor(backgroundRole(), QColor(255,255,255));
    ui->data->setPalette(palette);
    ui->data->setAutoFillBackground(true);
    //set font color
    QPalette pe;
    pe.setColor(QPalette::WindowText,Qt::white);
    ui->cTime->setPalette(pe);
    ui->title->setPalette(pe);
    //button style
    QPalette btnPe;
    btnPe.setColor(QPalette::ButtonText, Qt::white);
    ui->btnRst->setStyleSheet(blueBtn);
    ui->btnLht->setStyleSheet(greenBtn);
    ui->btnRst->setPalette(btnPe);
    ui->btnLht->setPalette(btnPe);
    //line edit
    ui->tDianya->setStyleSheet("QLineEdit {border:1px solid "
                               ";background-color:rgb(211,211,211);border-radius:5px;selection-color:pink}");
    ui->tMaikuan->setStyleSheet("QLineEdit {border:1px solid "
                               ";background-color:rgb(211,211,211);border-radius:5px;selection-color:pink}");
    ui->tCishu->setStyleSheet("QLineEdit {border:1px solid "
                               ";background-color:rgb(211,211,211);border-radius:5px;selection-color:pink}");
    ui->tJiange->setStyleSheet("QLineEdit {border:1px solid "
                               ";background-color:rgb(211,211,211);border-radius:5px;selection-color:pink}");
    ui->tLiusu->setStyleSheet("QLineEdit {border:1px solid "
                               ";background-color:rgb(211,211,211);border-radius:5px;selection-color:pink}");
    ui->tYaodong->setStyleSheet("QLineEdit {border:1px solid "
                                ";background-color:rgb(211,211,211);border-radius:5px;selection-color:pink}");

    QTimer *upTime = new QTimer(this);
    connect(upTime,SIGNAL(timeout()),this,SLOT(updateTime()));
    updateTime();
    upTime->start(1000);
}

ParamSetUI::~ParamSetUI()
{
    delete ui;
}

void ParamSetUI::updateTime()
{
    QDateTime curTime = QDateTime::currentDateTime();
    QString lblTm = curTime.date().toString("yyyy/MM/dd") + "\n " + curTime.time().toString("hh:mm:ss");
    this->ui->cTime->setText(lblTm);
}

void ParamSetUI::on_btnRst_clicked()
{
    this->hide();
    emit resetParam();
}

void ParamSetUI::on_btnLht_clicked()
{
    //use sqlite
    sqlite3 * pDB = 0;
    int nRes = sqlite3_open(dbPath.toStdString().c_str(), &pDB);
    if (nRes == SQLITE_OK)
    {
        QString qsQuery = "insert into hisData(tm, dy, mk, cs, jg, ls) values(datetime('now','localtime'), ";
        qsQuery += ParamUI::qstrDianya;
        qsQuery += ",";
        qsQuery += ParamUI::qstrMaikuan;
        qsQuery += ",";
        qsQuery += ParamUI::qstrCishu;
        qsQuery += ",";
        qsQuery += ParamUI::qstrJiange;
        qsQuery += ",";
        qsQuery += ParamUI::qstrLiusu;
        qsQuery += ");";
        char* cErrMsg;
        int nRes = sqlite3_exec(pDB , qsQuery.toStdString().c_str() ,0 ,0, &cErrMsg);
        if (nRes != SQLITE_OK)
        {

        }
    }
    //close
    sqlite3_close(pDB);

    this->hide();
    emit sendLight();
}

void ParamSetUI::getShow()
{
    this->show();
    this->activateWindow();
    this->raise();
    //set value
    ui->tDianya->setText(ParamUI::qstrDianya);
    ui->tMaikuan->setText(ParamUI::qstrMaikuan);
    ui->tCishu->setText(ParamUI::qstrCishu);
    ui->tJiange->setText(ParamUI::qstrJiange);
    ui->tLiusu->setText(ParamUI::qstrLiusu);
    //update timespan
    ParamUI::fTimeSpan = (float)(ParamUI::qstrMaikuan.toFloat()/1000000) +
            (float)(ParamUI::qstrJiange.toFloat()*ParamUI::qstrCishu.toInt()/1000);
    if (ParamUI::blYaodong)
    {
        ui->tYaodong->setText(tr("On"));
    }
    else
    {
        ui->tYaodong->setText(tr("Off"));
    }
}
